package top.lixunda.ecommerce.server.good.feign;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.good.api.entity.db.QiNiuFileDO;
import top.lixunda.ecommerce.server.good.api.feign.IFeignQiNiuFileController;
import top.lixunda.ecommerce.server.good.service.IQiNiuFileService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Xumda
 * @version time:2020/1/19 9:43
 */
@Slf4j
@RestController
public class FeignQiNiuFileController extends BaseController implements IFeignQiNiuFileController {

    private final IQiNiuFileService qiNiuFileService;

    @Autowired
    public FeignQiNiuFileController(IQiNiuFileService qiNiuFileService) {
        this.qiNiuFileService = qiNiuFileService;
    }

    /**
     * 根据id查询信息
     *
     * @param id id
     * @return 文件信息
     */
    @Override
    public MessageObject<QiNiuFileDO> queryById(Integer id) {
        if (ObjectValidators.isEmpty(id)) {
            throw getAppException("文件id不能为空");
        }
        QiNiuFileDO qiNiuFileDO = qiNiuFileService.getById(id);
        if (ObjectValidators.isEmpty(qiNiuFileDO)) {
            throw getAppException("文件不存在");
        }
        return getSuccessMessageObject("获取信息成功", qiNiuFileDO);
    }

    /**
     * 根据多个id查询信息
     *
     * @param ids id列表
     * @return 文件信息列表
     */
    @Override
    public MessageObject<List<QiNiuFileDO>> queryByIds(List<Integer> ids) {
        if (ObjectValidators.isEmpty(ids)) {
            throw getAppException("文件id不能为空");
        }
        List<QiNiuFileDO> qiNiuFileDoList = new ArrayList<>(qiNiuFileService.listByIds(ids));
        return getSuccessMessageObject("获取信息成功", qiNiuFileDoList);
    }

    /**
     * 添加一条七牛云文件上传信息
     *
     * @param qiNiuFileDO 七牛云文件上传回调信息
     * @return 添加后的文件数据，包含id
     */
    @Override
    public MessageObject<QiNiuFileDO> addQiNiuFileInfo(QiNiuFileDO qiNiuFileDO) {
        if (ObjectValidators.isEmpty(qiNiuFileDO)) {
            throw getAppException("数据不能为空");
        }
        if (ObjectValidators.isEmpty(qiNiuFileDO.getUserId())) {
            throw getAppException("userId不能为空");
        }
        qiNiuFileDO.setCreateTime(LocalDateTime.now());
        qiNiuFileService.save(qiNiuFileDO);
        return getSuccessMessageObject("保存成功", qiNiuFileDO);
    }

}
